package com.app.twth.ui.b2cpick.oblpn;


import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityB2cPickOblpnBinding;
import com.app.twth.ui.b2cpick.B2CPick;
import com.app.twth.ui.b2cpick.B2CPickViewModel;
import com.app.twth.ui.b2cpick.category.B2CPickCategoryActivity;
import com.app.twth.utils.EditTextUtil;

/**
 * @describe :
 */
public class B2CPickOBLPNActivity extends BaseActivity<ActivityB2cPickOblpnBinding, B2CPickViewModel> {
    private String TAG = getClass().getSimpleName();


    @Override
    protected void initListener() {
        viewModel.lpnLiveData.observe(this, data -> {
            viewModel.pickLiveData.getValue().setObLpn(data);
            Bundle bundle = new Bundle();
            bundle.putSerializable("B2CPick", viewModel.pickLiveData.getValue());
            AppRouter.showActivityForResult(mContext, B2CPickCategoryActivity.class, bundle, Constant.ForResult2);
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "Pick(OBLPN)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        viewModel.pickLiveData.setValue((B2CPick) bundle.getSerializable("B2CPick"));

        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                binding.tvCode.setText("");
                bottomLayoutBinding.tvHint.setText("");
                break;
        }
    }


    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
        viewModel.lpnCheck(barcodeStr);

//        viewModel.pickLiveData.getValue().setObLpn(barcodeStr);
//        Bundle bundle = new Bundle();
//        bundle.putSerializable("B2CPick",viewModel.pickLiveData.getValue());
//        AppRouter.showFragmentActivity(mContext, B2CPickCategoryActivity.class, bundle);
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                EditTextUtil.hideInput(mContext);
                viewModel.lpnCheck(barcodeStr);

//                viewModel.pickLiveData.getValue().setObLpn(barcodeStr);
//                Bundle bundle = new Bundle();
//                bundle.putSerializable("B2CPick",viewModel.pickLiveData.getValue());
//                AppRouter.showFragmentActivity(mContext, B2CPickCategoryActivity.class, bundle);
            }
            return false;
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ForResult2 && resultCode == RESULT_OK) {
            try {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
